package com.ycfmedical.bed.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.adapter.MusicListAdapter;
import com.ycfmedical.bed.bean.EntertainItem;
import com.ycfmedical.bed.ui.MusicPlayActivity;
import com.ycfmedical.bed.utils.ResourceUtil;

import android.os.Bundle;

import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.router.Router;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by Egoist on 2017/1/19.
 */

public class MusicFragment extends EntertainFragment
{
    MusicListAdapter mAdapter;

    @Override
    public SimpleRecAdapter getAdapter()
    {
        if (mAdapter == null)
        {
            mAdapter = new MusicListAdapter(context);
            mAdapter.setRecItemClick(new RecyclerItemCallback<EntertainItem, MusicListAdapter.ViewHolder>()
            {
                @Override
                public void onItemClick(int position, EntertainItem model, int tag, MusicListAdapter.ViewHolder holder)
                {
                    super.onItemClick(position, model, tag, holder);
                    // 将音乐列表传入到下播放页面
                    // 这里传递的是整个音乐列表，而不是单个音乐Item
                    if (!getData().isEmpty() && tag == mAdapter.TAG_ITEM)
                    {
                        ArrayList<EntertainItem> list = new ArrayList<EntertainItem>();
                        // 数据源不为空才执行序列化
                        list.addAll(getData());
                        //Bundle bundle = new Bundle();
                        // Parcelable序列化 键值对传递列表
                        //bundle.putParcelableArrayList(Constants.MUSICLIST, list);
                        // 路由器带值跳转
                        Router.newIntent(context).to(MusicPlayActivity.class).launch();
                    }
                }
            });
            
        }
        return mAdapter;
    }
    
    // 从父类的方法中获得数据源
    private List<EntertainItem> getData()
    {
        return mAdapter.getDataSource();
    }
    
    @Override
    public void setLayoutManager(XRecyclerView recyclerView)
    {
        recyclerView.verticalLayoutManager(context);
    }
    
    @Override
    public String getType()
    {
        return ResourceUtil.getResources().getString(R.string.entertain_tab_music);
    }
    
    public static MusicFragment newInstance()
    {
        return new MusicFragment();
    }
    
}
